package com.es.www.kyl.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.es.www.kyl.service.UploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Estranger
 * @date 2025/9/2
 * description
 */
@Service
public class UploadServiceImpl implements UploadService {

	@Value("${file.upload.path}")
	private String fileUploadPath;
	@Override
	public String upload(MultipartFile file) throws IOException {
		String originalFilename = file.getOriginalFilename();
		String fileExtension = FileUtil.extName(originalFilename);
		String uuid = IdUtil.simpleUUID();
		String newFileName = uuid + "." + fileExtension;
		// 组装文件在服务器上的完整路径
		String filePath = fileUploadPath + newFileName;
		FileUtil.writeBytes(file.getBytes(), filePath);
		return "/uploads/" + newFileName;
	}
}
